<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 This example was done using Colab. You can read and use the Python code in other environments if you have never used Colab before. For example, we used a path to a file in gs
Step1:  Download a file of DeepVariant examples from Google Cloud Storage to the local file storage of this notebook. DeepVariant examples are stored in TFRecord format.
Step2:  Show one DeepVariant example
We will use the nucleus.util.vis module to inspect and visualize a single DeepVariant example.
Step3:  You can also save the images as .png files
Step4:  This creates 10 PNG images labeled with locus IDs and truth labels.
Step5:  Flexible, modular functions
The nucleus.util.vis library also has smaller building blocks you can use if you need more flexibility.
<ASSISTANT_TASK:>
Python Code:
# Nucleus is the only dependency we need to install. We need at least
# nucleus 0.5.0, since this was the first release of Nucleus that includes the
# visualization utilities we are using here.
%%capture
! pip install google-nucleus==0.5.1
# Nucleus may show an error about failing to build a wheel, but it still
# installs successfully.
import tensorflow as tf
from nucleus.util import vis
print(tf.__version__)
Explanation: This example was done using Colab. You can read and use the Python code in other environments if you have never used Colab before. For example, we used a path to a file in gs://, but you can also just copy that file to your environment and use a local path instead.
This example is for advanced users who want to understand the data representation. You do NOT need to understand this in order to use DeepVariant.
<table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https://colab.research.google.com/github/google/deepvariant/blob/r1.4/docs/visualizing_examples.ipynb"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab</a>
  </td>
  <td>
    <a target="_blank" href="https://github.com/google/deepvariant/blob/r1.4/docs/visualizing_examples.ipynb"><img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" />View source on GitHub</a>
  </td>
</table>
Note: This notebook was updated in the 1.0 release to use the new nucleus.util.vis module that makes visualizing DeepVariant examples easier. The old notebook can be found at https://github.com/google/deepvariant/blob/r0.10/docs/visualizing_examples.ipynb
Visualizing DeepVariant examples
DeepVariant represents putative variants as pileup image tensors and classifies them using a convolutional neural network.
In this notebook, we will inspect some DeepVariant examples and visualize the pileup images.
We (the DeepVariant team) also wrote a blog post that explains what these pileup images are and how they are used in DeepVariant:
https://google.github.io/deepvariant/posts/2020-02-20-looking-through-deepvariants-eyes/
End of explanation
!gsutil -q cp gs://deepvariant/datalab-testdata/make_examples_datalab.tfrecord.gz /tmp/make_examples_colab.tfrecord.gz
examples_path = '/tmp/make_examples_colab.tfrecord.gz'
Explanation: Download a file of DeepVariant examples from Google Cloud Storage to the local file storage of this notebook. DeepVariant examples are stored in TFRecord format.
End of explanation
dataset = tf.data.TFRecordDataset(examples_path, compression_type="GZIP")
# Take a single example and show the deepvariant pileup image for it.
for e in dataset.take(1):
  example = tf.train.Example()
  example.ParseFromString(e.numpy())
  # For example, in chr20:19503712_T_C, T is the reference allele and C is the
  # alternate allele that is proposed in this pileup image.
  print("Locus ID with alt:", vis.locus_id_with_alt(example))
  print("Draw the channels:")
  vis.draw_deepvariant_pileup(example)
  print("Truth label:", vis.label_from_example(example))
Explanation: Show one DeepVariant example
We will use the nucleus.util.vis module to inspect and visualize a single DeepVariant example.
End of explanation
for e in dataset.take(10):
  example = tf.train.Example()
  example.ParseFromString(e.numpy())
  # For example, in chr20:19503712_T_C, T is the reference allele and C is the
  # alternate allele that is proposed in this pileup image.
  filename = 'pileup_{}_truth={}.png'.format(vis.locus_id_with_alt(example), vis.label_from_example(example))
  vis.draw_deepvariant_pileup(example, path=filename, show=False)
Explanation: You can also save the images as .png files
End of explanation
! ls
Explanation: This creates 10 PNG images labeled with locus IDs and truth labels.
End of explanation
for e in dataset.take(1):
  example = tf.train.Example()
  example.ParseFromString(e.numpy())
  print("Extracting the variant:")
  print(vis.variant_from_example(example))
  # Get the pileup image as an array.
  arr = vis.get_image_array_from_example(example)
  print("\nShape of the pileup numpy array:", arr.shape)
  # Split that array into channels.
  channels = vis.split_3d_array_into_channels(arr)
  print("\nNumber of channels:", len(channels))
  # Visualize the numpy array as an image.
  print("\nShowing just the first channel:")
  vis.array_to_png(channels[0])
Explanation: Flexible, modular functions
The nucleus.util.vis library also has smaller building blocks you can use if you need more flexibility.
End of explanation
<END_TASK>