annotate data_manager/vsnp_genbank_fetcher.py @ 2:df67ba6db5de draft default tip

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