annotate data_manager/pima_pv_downloader.py @ 0:9972e1e07347 draft default tip

Uploaded
author greg
date Mon, 12 Dec 2022 15:02:14 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9972e1e07347 Uploaded
greg
parents:
diff changeset
1 import argparse
9972e1e07347 Uploaded
greg
parents:
diff changeset
2 import datetime
9972e1e07347 Uploaded
greg
parents:
diff changeset
3 import json
9972e1e07347 Uploaded
greg
parents:
diff changeset
4 import os
9972e1e07347 Uploaded
greg
parents:
diff changeset
5 import sys
9972e1e07347 Uploaded
greg
parents:
diff changeset
6 from urllib.request import Request, urlopen
9972e1e07347 Uploaded
greg
parents:
diff changeset
7
9972e1e07347 Uploaded
greg
parents:
diff changeset
8
9972e1e07347 Uploaded
greg
parents:
diff changeset
9 def url_download(url, workdir):
9972e1e07347 Uploaded
greg
parents:
diff changeset
10 file_path = os.path.abspath(os.path.join(workdir, os.path.basename(url)))
9972e1e07347 Uploaded
greg
parents:
diff changeset
11 src = None
9972e1e07347 Uploaded
greg
parents:
diff changeset
12 dst = None
9972e1e07347 Uploaded
greg
parents:
diff changeset
13 try:
9972e1e07347 Uploaded
greg
parents:
diff changeset
14 req = Request(url)
9972e1e07347 Uploaded
greg
parents:
diff changeset
15 src = urlopen(req)
9972e1e07347 Uploaded
greg
parents:
diff changeset
16 with open(file_path, 'wb') as dst:
9972e1e07347 Uploaded
greg
parents:
diff changeset
17 while True:
9972e1e07347 Uploaded
greg
parents:
diff changeset
18 chunk = src.read(2**10)
9972e1e07347 Uploaded
greg
parents:
diff changeset
19 if chunk:
9972e1e07347 Uploaded
greg
parents:
diff changeset
20 dst.write(chunk)
9972e1e07347 Uploaded
greg
parents:
diff changeset
21 else:
9972e1e07347 Uploaded
greg
parents:
diff changeset
22 break
9972e1e07347 Uploaded
greg
parents:
diff changeset
23 except Exception as e:
9972e1e07347 Uploaded
greg
parents:
diff changeset
24 sys.exit(str(e))
9972e1e07347 Uploaded
greg
parents:
diff changeset
25 finally:
9972e1e07347 Uploaded
greg
parents:
diff changeset
26 if src:
9972e1e07347 Uploaded
greg
parents:
diff changeset
27 src.close()
9972e1e07347 Uploaded
greg
parents:
diff changeset
28 return file_path
9972e1e07347 Uploaded
greg
parents:
diff changeset
29
9972e1e07347 Uploaded
greg
parents:
diff changeset
30
9972e1e07347 Uploaded
greg
parents:
diff changeset
31 def download(url, out_file):
9972e1e07347 Uploaded
greg
parents:
diff changeset
32 today = datetime.datetime.utcnow().strftime("%Y-%m-%d")
9972e1e07347 Uploaded
greg
parents:
diff changeset
33
9972e1e07347 Uploaded
greg
parents:
diff changeset
34 with open(out_file) as fh:
9972e1e07347 Uploaded
greg
parents:
diff changeset
35 params = json.load(fh)
9972e1e07347 Uploaded
greg
parents:
diff changeset
36
9972e1e07347 Uploaded
greg
parents:
diff changeset
37 workdir = params['output_data'][0]['extra_files_path']
9972e1e07347 Uploaded
greg
parents:
diff changeset
38 os.makedirs(workdir)
9972e1e07347 Uploaded
greg
parents:
diff changeset
39 file_path = url_download(url, workdir)
9972e1e07347 Uploaded
greg
parents:
diff changeset
40 name = '%s_%s' % (today, os.path.basename(file_path))
9972e1e07347 Uploaded
greg
parents:
diff changeset
41
9972e1e07347 Uploaded
greg
parents:
diff changeset
42 data_manager_json = {"data_tables": {}}
9972e1e07347 Uploaded
greg
parents:
diff changeset
43 data_manager_entry = {}
9972e1e07347 Uploaded
greg
parents:
diff changeset
44 data_manager_entry['value'] = today
9972e1e07347 Uploaded
greg
parents:
diff changeset
45 data_manager_entry['name'] = name
9972e1e07347 Uploaded
greg
parents:
diff changeset
46 data_manager_entry['path'] = file_path
9972e1e07347 Uploaded
greg
parents:
diff changeset
47 data_manager_json["data_tables"]["pima_pv"] = data_manager_entry
9972e1e07347 Uploaded
greg
parents:
diff changeset
48
9972e1e07347 Uploaded
greg
parents:
diff changeset
49 with open(out_file, 'w') as fh:
9972e1e07347 Uploaded
greg
parents:
diff changeset
50 json.dump(data_manager_json, fh, sort_keys=True)
9972e1e07347 Uploaded
greg
parents:
diff changeset
51
9972e1e07347 Uploaded
greg
parents:
diff changeset
52
9972e1e07347 Uploaded
greg
parents:
diff changeset
53 parser = argparse.ArgumentParser()
9972e1e07347 Uploaded
greg
parents:
diff changeset
54
9972e1e07347 Uploaded
greg
parents:
diff changeset
55 parser.add_argument('--url', dest='url', help='URL to download plasmids_and_vectors.fasta file')
9972e1e07347 Uploaded
greg
parents:
diff changeset
56 parser.add_argument('--out_file', dest='out_file', help='JSON output file')
9972e1e07347 Uploaded
greg
parents:
diff changeset
57
9972e1e07347 Uploaded
greg
parents:
diff changeset
58 args = parser.parse_args()
9972e1e07347 Uploaded
greg
parents:
diff changeset
59
9972e1e07347 Uploaded
greg
parents:
diff changeset
60 download(args.url, args.out_file)