Mercurial > repos > card > rgi
annotate data_managers/data_manager_rgi_build_db/data_manager/rgi_database_builder.py @ 3:bfbfc24c5af2 draft
"planemo upload for repository https://github.com/arpcard/rgi commit 101f923fe5bb3fb80716adb5320b96cef8ae2386"
author | card |
---|---|
date | Fri, 20 Nov 2020 18:57:39 +0000 |
parents | 715bc9aeef69 |
children |
rev | line source |
---|---|
0
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
1 import argparse |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
2 import datetime |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
3 import json |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
4 import os |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
5 import shutil |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
6 import sys |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
7 import tarfile |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
8 import urllib.request, urllib.error, urllib.parse |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
9 import zipfile |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
10 from import_data import run, _main |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
11 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
12 parser = argparse.ArgumentParser(description='Create data manager json.') |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
13 parser.add_argument('--url', dest='url', action='store', help='Url for CARD data') |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
14 parser.add_argument('--out', dest='output', action='store', help='JSON filename') |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
15 parser.add_argument('--name', dest='name', action='store', default='CARD_data-' + str(datetime.datetime.now().strftime('%Y-%B-%d-%H:%M:%S')), help='Data table database name') |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
16 args = parser.parse_args() |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
17 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
18 print('[rgi_database_builder] Importing...') |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
19 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
20 _main(args) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
21 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
22 def main(args): |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
23 print('[rgi_database_builder] Building......') |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
24 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
25 data_manager_entry = {} |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
26 data_manager_entry['value'] = args.name.lower() |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
27 data_manager_entry['name'] = args.name |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
28 data_manager_entry['path'] = '.' |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
29 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
30 data_manager_json = dict(data_tables = dict(rgi_databases=data_manager_entry)) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
31 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
32 params = json.loads(open(args.output,'r').read()) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
33 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
34 target_directory = params['output_data'][0]['extra_files_path'] |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
35 os.mkdir(target_directory) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
36 output_path = os.path.join(os.getcwd(), 'rgi-database') |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
37 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
38 for filename in os.listdir(output_path): |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
39 print('[rgi_database_builder] move file: {} from {} to {}'.format(filename, output_path, target_directory)) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
40 shutil.move(os.path.join(output_path, filename), target_directory) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
41 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
42 print(args.output) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
43 print('[rgi_database_builder] write file: {}'.format(args.output)) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
44 open(args.output, 'w').write(json.dumps(data_manager_json)) |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
45 |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
46 if __name__ == '__main__': |
715bc9aeef69
planemo upload for repository https://github.com/arpcard/rgi commit 7a78289be23c5a14ae39f454610fa8eca3f05188
card
parents:
diff
changeset
|
47 main(args) |