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