Skip to content

Commit c5274b7

Browse files
Nathan WatsonNathan Watson
authored andcommitted
New script eu_search_results_json.py
1 parent 080ee87 commit c5274b7

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
###
5+
# © 2018 The Board of Trustees of the Leland Stanford Junior University
6+
# Nathaniel Watson
7+
# nathankw@stanford.edu
8+
###
9+
10+
"""
11+
Provided a search URL that is specific to the ENCODE Portal, saves the search results as JSON
12+
in the specified output file. The search results are stored as a list of JSON objects.
13+
14+
|
15+
"""
16+
17+
import argparse
18+
import json
19+
import os
20+
import sys
21+
22+
import encode_utils.connection as euc
23+
from encode_utils.parent_argparser import dcc_login_parser
24+
25+
# Check that Python3 is being used
26+
v = sys.version_info
27+
if v < (3, 3):
28+
raise Exception("Requires Python 3.3 or greater.")
29+
30+
def get_parser():
31+
parser = argparse.ArgumentParser(
32+
description = __doc__,
33+
parents=[dcc_login_parser],
34+
formatter_class=argparse.RawTextHelpFormatter)
35+
parser.add_argument("-l", "--limit", type=int, help="The number of search results to get back. Leave blank if you want all.")
36+
parser.add_argument("-u", "--url", required=True, help="The ENCODE Portal URL that you use for searching. Wrap this in quotes to make sure its all treated as one argument.")
37+
parser.add_argument("-o", "--outfile", required=True, help="The JSON output file containing the search results.")
38+
return parser
39+
40+
def main():
41+
parser = get_parser()
42+
args = parser.parse_args()
43+
dcc_mode = args.dcc_mode
44+
limit = args.limit
45+
outfile = args.outfile
46+
url = args.url
47+
48+
if dcc_mode:
49+
conn = euc.Connection(dcc_mode)
50+
else:
51+
# Default dcc_mode taken from environment variable DCC_MODE.
52+
conn = euc.Connection()
53+
54+
fout = open(outfile, "w")
55+
results = conn.search(limit=limit, url=url) #returns a list of search results
56+
fout.write(json.dumps(results))
57+
fout.close()
58+
59+
60+
if __name__ == "__main__":
61+
main()

0 commit comments

Comments
 (0)