Skip to content

Instantly share code, notes, and snippets.

@seandavi
Forked from pgrosu/Python_client_secrets.py
Created August 18, 2016 06:54
Show Gist options
  • Save seandavi/3308cf86bdc0ed9959118b650bdece4f to your computer and use it in GitHub Desktop.
Save seandavi/3308cf86bdc0ed9959118b650bdece4f to your computer and use it in GitHub Desktop.
Python and R code examples (with results) using Google Genomics API and a client_secrets.json file
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/cache-account-1.json
SCOPES = 'https://www.googleapis.com/auth/genomics'
CLIENT_SECRET_FILE = 'client_secrets.json'
APPLICATION_NAME = 'Google Genomics Test'
def get_credentials():
#Gets valid user credentials from storage.
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'cache-account-1.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store)
return credentials
def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('genomics', 'v1', http=http)
dataset_id = '10473108253681171589' # This is the 1000 Genomes dataset ID
sample = 'NA12872'
request = service.readgroupsets().search(
body={'datasetIds': [dataset_id], 'name': sample},
fields='readGroupSets(id)')
read_group_sets = request.execute().get('readGroupSets', [])
print(read_group_sets)
if __name__ == '__main__':
main()
$ python genomics.py --noauth_local_webserver
[{u'id': u'CMvnhpKTFhDu5oWzv-LJtIEB'}]
library(httr)
library(rjson)
auth_genomics <- function(client_secrets_file="client_secrets.json") {
clientSecrets <- fromJSON(file=client_secrets_file)
clientId <- clientSecrets$installed$client_id
clientSecret <- clientSecrets$installed$client_secret
# Get oauth token.
endpoint <- oauth_endpoints("google")
scope <- "https://www.googleapis.com/auth/genomics"
app <- oauth_app("google", clientId, clientSecret)
google_token <- oauth2.0_token(
endpoint, app,
scope=scope,
use_oob=FALSE,
cache="cache-account-1")
google_token
}
google_token <- auth_genomics()
google_genomics_endpoint <- "https://genomics.googleapis.com/v1"
dataset_id <- '10473108253681171589' # This is the 1000 Genomes dataset ID
sampleName <- 'NA12872'
requestBody <- list(datasetIds=list('10473108253681171589'), name=sampleName)
queryParams <- list(fields="readGroupSets(id)")
response <- POST(paste(google_genomics_endpoint,
"readgroupsets/search",
sep="/"),
query=queryParams,
body=toJSON(requestBody),
config(token=google_token),
add_headers("Content-Type"="application/json"))
content(response)
> content(response)
$readGroupSets
$readGroupSets[[1]]
$readGroupSets[[1]]$id
[1] "CMvnhpKTFhDu5oWzv-LJtIEB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment