annotate data_manager/vsnp_dnaprints_fetcher.py @ 2:50c71f5a632c draft default tip

Uploaded
author greg
date Tue, 05 May 2020 15:13:45 -0400
parents f58178a5eebc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f58178a5eebc Uploaded
greg
parents:
diff changeset
1 import argparse
f58178a5eebc Uploaded
greg
parents:
diff changeset
2 import json
f58178a5eebc Uploaded
greg
parents:
diff changeset
3 import os
f58178a5eebc Uploaded
greg
parents:
diff changeset
4 import sys
f58178a5eebc Uploaded
greg
parents:
diff changeset
5 try:
f58178a5eebc Uploaded
greg
parents:
diff changeset
6 # For Python 3.0 and later
f58178a5eebc Uploaded
greg
parents:
diff changeset
7 from urllib.request import Request, urlopen
f58178a5eebc Uploaded
greg
parents:
diff changeset
8 except ImportError:
f58178a5eebc Uploaded
greg
parents:
diff changeset
9 # Fall back to Python 2 imports
f58178a5eebc Uploaded
greg
parents:
diff changeset
10 from urllib2 import Request, urlopen
f58178a5eebc Uploaded
greg
parents:
diff changeset
11
f58178a5eebc Uploaded
greg
parents:
diff changeset
12
f58178a5eebc Uploaded
greg
parents:
diff changeset
13 def url_download(url, workdir):
f58178a5eebc Uploaded
greg
parents:
diff changeset
14 file_path = os.path.abspath(os.path.join(workdir, os.path.basename(url)))
f58178a5eebc Uploaded
greg
parents:
diff changeset
15 src = None
f58178a5eebc Uploaded
greg
parents:
diff changeset
16 dst = None
f58178a5eebc Uploaded
greg
parents:
diff changeset
17 try:
f58178a5eebc Uploaded
greg
parents:
diff changeset
18 req = Request(url)
f58178a5eebc Uploaded
greg
parents:
diff changeset
19 src = urlopen(req)
f58178a5eebc Uploaded
greg
parents:
diff changeset
20 with open(file_path, 'wb') as dst:
f58178a5eebc Uploaded
greg
parents:
diff changeset
21 while True:
f58178a5eebc Uploaded
greg
parents:
diff changeset
22 chunk = src.read(2**10)
f58178a5eebc Uploaded
greg
parents:
diff changeset
23 if chunk:
f58178a5eebc Uploaded
greg
parents:
diff changeset
24 dst.write(chunk)
f58178a5eebc Uploaded
greg
parents:
diff changeset
25 else:
f58178a5eebc Uploaded
greg
parents:
diff changeset
26 break
f58178a5eebc Uploaded
greg
parents:
diff changeset
27 except Exception as e:
f58178a5eebc Uploaded
greg
parents:
diff changeset
28 sys.exit(str(e))
f58178a5eebc Uploaded
greg
parents:
diff changeset
29 finally:
f58178a5eebc Uploaded
greg
parents:
diff changeset
30 if src:
f58178a5eebc Uploaded
greg
parents:
diff changeset
31 src.close()
f58178a5eebc Uploaded
greg
parents:
diff changeset
32 return file_path
f58178a5eebc Uploaded
greg
parents:
diff changeset
33
f58178a5eebc Uploaded
greg
parents:
diff changeset
34
f58178a5eebc Uploaded
greg
parents:
diff changeset
35 def download(dbkey, name, url, out_file):
f58178a5eebc Uploaded
greg
parents:
diff changeset
36
f58178a5eebc Uploaded
greg
parents:
diff changeset
37 with open(out_file) as fh:
f58178a5eebc Uploaded
greg
parents:
diff changeset
38 params = json.loads(fh.read())
f58178a5eebc Uploaded
greg
parents:
diff changeset
39
f58178a5eebc Uploaded
greg
parents:
diff changeset
40 workdir = params['output_data'][0]['extra_files_path']
f58178a5eebc Uploaded
greg
parents:
diff changeset
41 os.makedirs(workdir)
f58178a5eebc Uploaded
greg
parents:
diff changeset
42 file_path = url_download(url, workdir)
f58178a5eebc Uploaded
greg
parents:
diff changeset
43 entry_name = os.path.basename(file_path)
f58178a5eebc Uploaded
greg
parents:
diff changeset
44
f58178a5eebc Uploaded
greg
parents:
diff changeset
45 data_manager_json = {"data_tables": {}}
f58178a5eebc Uploaded
greg
parents:
diff changeset
46 data_manager_entry = {}
f58178a5eebc Uploaded
greg
parents:
diff changeset
47 data_manager_entry['value'] = dbkey
f58178a5eebc Uploaded
greg
parents:
diff changeset
48 data_manager_entry['name'] = entry_name
f58178a5eebc Uploaded
greg
parents:
diff changeset
49 data_manager_entry['path'] = file_path
f58178a5eebc Uploaded
greg
parents:
diff changeset
50 data_manager_entry['description'] = "DNAprints file for %s" % name
f58178a5eebc Uploaded
greg
parents:
diff changeset
51 data_manager_json["data_tables"]["vsnp_dnaprints"] = data_manager_entry
f58178a5eebc Uploaded
greg
parents:
diff changeset
52
f58178a5eebc Uploaded
greg
parents:
diff changeset
53 with open(out_file, 'w') as fh:
f58178a5eebc Uploaded
greg
parents:
diff changeset
54 fh.write(json.dumps(data_manager_json, sort_keys=True))
f58178a5eebc Uploaded
greg
parents:
diff changeset
55
f58178a5eebc Uploaded
greg
parents:
diff changeset
56
f58178a5eebc Uploaded
greg
parents:
diff changeset
57 parser = argparse.ArgumentParser()
f58178a5eebc Uploaded
greg
parents:
diff changeset
58
f58178a5eebc Uploaded
greg
parents:
diff changeset
59 parser.add_argument('--dbkey', dest='dbkey', help='Genome reference dbkey')
f58178a5eebc Uploaded
greg
parents:
diff changeset
60 parser.add_argument('--name', dest='name', help='Reference display name')
f58178a5eebc Uploaded
greg
parents:
diff changeset
61 parser.add_argument('--url', dest='url', help='URL to download DNAprints file')
f58178a5eebc Uploaded
greg
parents:
diff changeset
62 parser.add_argument('--out_file', dest='out_file', help='JSON output file')
f58178a5eebc Uploaded
greg
parents:
diff changeset
63
f58178a5eebc Uploaded
greg
parents:
diff changeset
64 args = parser.parse_args()
f58178a5eebc Uploaded
greg
parents:
diff changeset
65
f58178a5eebc Uploaded
greg
parents:
diff changeset
66 download(args.dbkey, args.name, args.url, args.out_file)