<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <!-- Copyright 2015 Google Inc. All rights reserved. -->
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
<!-- you may not use this file except in compliance with the License. -->
<!-- You may obtain a copy of the License at -->
<!--     http
Step1:  Create an Authenticated Client
Next we construct a Python object that we can use it to make requests. 
The following snippet shows how we can authenticate using the service account on the Datalab host.  For more detail about authentication from Python, see Using OAuth 2.0 for Server to Server Applications.
Step2:  And then we create a client for the Genomics API.
Step3:  Send a request to the Genomics API
Now that we have a Python client for the Genomics API, we can access a variety of different resources. For details about each available resource, see the python client API docs here.
Using our genomics client, we'll demonstrate fetching a Dataset resource by ID (the 1000 Genomes dataset in this case).
First, we need to construct a request object.
Step4:  Next, we'll send this request to the Genomics API by calling the request.execute() method.
Step5:  You will need enable the Genomics API for your project if you have not done so previously.  Click on this link to enable the API in your project.
The response object returned is simply a Python dictionary. Let's take a look at the properties returned in the response.
Step6:  Success! We can see the name of the specified Dataset and a few other pieces of metadata.
Accessing other Genomics API resources will follow this same set of steps. The full list of available resources within the API is here. Each resource has details about the different verbs that can be applied (e.g., Dataset methods).
Access Data
In this portion of the notebook, we implement this same example implemented as a python script.  First let's define a few constants to use within the examples that follow.
Step7:  Get read bases for a sample at specific a position
First find the read group set ID for the sample.
Step8:  Once we have the read group set ID, lookup the reads at the position in which we are interested.
Step9:  And we print out the results.
Step10:  Get variants for a sample at specific a position
First find the call set ID for the sample.
Step11:  Once we have the call set ID, lookup the variants that overlap the position in which we are interested.
Step12:  And we print out the results.
<ASSISTANT_TASK:>
Python Code:
!pip install --upgrade google-api-python-client
Explanation: <!-- Copyright 2015 Google Inc. All rights reserved. -->
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
<!-- you may not use this file except in compliance with the License. -->
<!-- You may obtain a copy of the License at -->
<!--     http://www.apache.org/licenses/LICENSE-2.0 -->
<!-- Unless required by applicable law or agreed to in writing, software -->
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
<!-- See the License for the specific language governing permissions and -->
<!-- limitations under the License. -->
Getting started with the Google Genomics API
In this notebook we'll cover how to make authenticated requests to the Google Genomics API.
NOTE:
If you're new to notebooks, or want to check out additional samples, check out the full list of general notebooks.
For additional Genomics samples, check out the full list of Genomics notebooks.
Setup
Install Python libraries
We'll be using the Google Python API client for interacting with Genomics API. We can install this library, or any other 3rd-party Python libraries from the Python Package Index (PyPI) using the pip package manager.
There are 50+ Google APIs that you can work against with the Google Python API Client, but we'll focus on the Genomics API in this notebook.
End of explanation
from httplib2 import Http
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
http = Http()
credentials.authorize(http)
Explanation: Create an Authenticated Client
Next we construct a Python object that we can use it to make requests. 
The following snippet shows how we can authenticate using the service account on the Datalab host.  For more detail about authentication from Python, see Using OAuth 2.0 for Server to Server Applications.
End of explanation
from apiclient.discovery import build
genomics = build('genomics', 'v1', http=http)
Explanation: And then we create a client for the Genomics API.
End of explanation
request = genomics.datasets().get(datasetId='10473108253681171589')
Explanation: Send a request to the Genomics API
Now that we have a Python client for the Genomics API, we can access a variety of different resources. For details about each available resource, see the python client API docs here.
Using our genomics client, we'll demonstrate fetching a Dataset resource by ID (the 1000 Genomes dataset in this case).
First, we need to construct a request object.
End of explanation
response = request.execute()
Explanation: Next, we'll send this request to the Genomics API by calling the request.execute() method.
End of explanation
for entry in response.items():
    print "%s => %s" % entry
Explanation: You will need enable the Genomics API for your project if you have not done so previously.  Click on this link to enable the API in your project.
The response object returned is simply a Python dictionary. Let's take a look at the properties returned in the response.
End of explanation
dataset_id = '10473108253681171589' # This is the 1000 Genomes dataset ID
sample = 'NA12872'
reference_name = '22'
reference_position = 51003835
Explanation: Success! We can see the name of the specified Dataset and a few other pieces of metadata.
Accessing other Genomics API resources will follow this same set of steps. The full list of available resources within the API is here. Each resource has details about the different verbs that can be applied (e.g., Dataset methods).
Access Data
In this portion of the notebook, we implement this same example implemented as a python script.  First let's define a few constants to use within the examples that follow.
End of explanation
request = genomics.readgroupsets().search(
  body={'datasetIds': [dataset_id], 'name': sample},
  fields='readGroupSets(id)')
read_group_sets = request.execute().get('readGroupSets', [])
if len(read_group_sets) != 1:
  raise Exception('Searching for %s didn\'t return '
                  'the right number of read group sets' % sample)
read_group_set_id = read_group_sets[0]['id']
Explanation: Get read bases for a sample at specific a position
First find the read group set ID for the sample.
End of explanation
request = genomics.reads().search(
  body={'readGroupSetIds': [read_group_set_id],
        'referenceName': reference_name,
        'start': reference_position,
        'end': reference_position + 1,
        'pageSize': 1024},
  fields='alignments(alignment,alignedSequence)')
reads = request.execute().get('alignments', [])
Explanation: Once we have the read group set ID, lookup the reads at the position in which we are interested.
End of explanation
# Note: This is simplistic - the cigar should be considered for real code
bases = [read['alignedSequence'][
           reference_position - int(read['alignment']['position']['position'])]
         for read in reads]
print '%s bases on %s at %d are' % (sample, reference_name, reference_position)
from collections import Counter
for base, count in Counter(bases).items():
  print '%s: %s' % (base, count)
Explanation: And we print out the results.
End of explanation
request = genomics.callsets().search(
  body={'variantSetIds': [dataset_id], 'name': sample},
  fields='callSets(id)')
resp = request.execute()
call_sets = resp.get('callSets', [])
if len(call_sets) != 1:
  raise Exception('Searching for %s didn\'t return '
                  'the right number of call sets' % sample)
call_set_id = call_sets[0]['id']
Explanation: Get variants for a sample at specific a position
First find the call set ID for the sample.
End of explanation
request = genomics.variants().search(
  body={'callSetIds': [call_set_id],
        'referenceName': reference_name,
        'start': reference_position,
        'end': reference_position + 1},
  fields='variants(names,referenceBases,alternateBases,calls(genotype))')
variant = request.execute().get('variants', [])[0]
Explanation: Once we have the call set ID, lookup the variants that overlap the position in which we are interested.
End of explanation
variant_name = variant['names'][0]
genotype = [variant['referenceBases'] if g == 0
            else variant['alternateBases'][g - 1]
            for g in variant['calls'][0]['genotype']]
print 'the called genotype is %s for %s' % (','.join(genotype), variant_name)
Explanation: And we print out the results.
End of explanation
<END_TASK>