-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigquery_query.py
More file actions
35 lines (24 loc) · 944 Bytes
/
bigquery_query.py
File metadata and controls
35 lines (24 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import sys
import subprocess
from google.cloud import bigquery
def shell_do(command, log=False, return_log=False):
print(f'Executing: {(" ").join(command.split())}', file=sys.stderr)
res=subprocess.run(command.split(), stdout=subprocess.PIPE)
if log:
print(res.stdout.decode('utf-8'))
if return_log:
return(res.stdout.decode('utf-8'))
if __name__ == '__main__':
# set credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'genotools-02f64a1e10be.json'
# initialize big query client
client = bigquery.Client()
# define metrics table
metrics_table = 'genotools.snp_metrics.metrics'
# test query
test_query = f'SELECT * FROM `{metrics_table}` WHERE maf IS NOT NULL AND maf != 0.0 and maf != 0.0625 LIMIT 100'
print(test_query)
# query from gcloud and print results
query_results = client.query(test_query).to_dataframe()
print(query_results)