Mercurial > repos > devteam > data_manager_fetch_ncbi_taxonomy
annotate data_manager/data_manager.py @ 4:cef5c909ccb8 draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 79946a95eb9a4c98fde04fa2cb3bc6a31a28e407"
author | iuc |
---|---|
date | Tue, 07 Jul 2020 00:05:59 +0000 |
parents | f7b443a73374 |
children | eaca3e270bf6 |
rev | line source |
---|---|
0
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
1 import argparse |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
2 import datetime |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
3 import json |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
4 import os |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
5 import shutil |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
6 import tarfile |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
7 import zipfile |
4
cef5c909ccb8
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 79946a95eb9a4c98fde04fa2cb3bc6a31a28e407"
iuc
parents:
3
diff
changeset
|
8 try: |
cef5c909ccb8
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 79946a95eb9a4c98fde04fa2cb3bc6a31a28e407"
iuc
parents:
3
diff
changeset
|
9 # For Python 3.0 and later |
cef5c909ccb8
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 79946a95eb9a4c98fde04fa2cb3bc6a31a28e407"
iuc
parents:
3
diff
changeset
|
10 from urllib.request import Request, urlopen |
cef5c909ccb8
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 79946a95eb9a4c98fde04fa2cb3bc6a31a28e407"
iuc
parents:
3
diff
changeset
|
11 except ImportError: |
cef5c909ccb8
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 79946a95eb9a4c98fde04fa2cb3bc6a31a28e407"
iuc
parents:
3
diff
changeset
|
12 # Fall back to Python 2 imports |
cef5c909ccb8
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 79946a95eb9a4c98fde04fa2cb3bc6a31a28e407"
iuc
parents:
3
diff
changeset
|
13 from urllib2 import Request, urlopen |
0
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
14 |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
15 |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
16 def url_download(url, workdir): |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
17 file_path = os.path.join(workdir, 'download.dat') |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
18 if not os.path.exists(workdir): |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
19 os.makedirs(workdir) |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
20 src = None |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
21 dst = None |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
22 try: |
1
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
23 req = Request(url) |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
24 src = urlopen(req) |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
25 with open(file_path, 'wb') as dst: |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
26 while True: |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
27 chunk = src.read(2**10) |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
28 if chunk: |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
29 dst.write(chunk) |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
30 else: |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
31 break |
0
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
32 finally: |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
33 if src: |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
34 src.close() |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
35 if tarfile.is_tarfile(file_path): |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
36 fh = tarfile.open(file_path, 'r:*') |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
37 elif zipfile.is_zipfile(file_path): |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
38 fh = zipfile.ZipFile(file_path, 'r') |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
39 else: |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
40 return |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
41 fh.extractall(workdir) |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
42 os.remove(file_path) |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
43 |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
44 |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
45 def main(args): |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
46 workdir = os.path.join(os.getcwd(), 'taxonomy') |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
47 url_download(args.url, workdir) |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
48 data_manager_entry = {} |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
49 data_manager_entry['value'] = args.name.lower() |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
50 data_manager_entry['name'] = args.name |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
51 data_manager_entry['path'] = '.' |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
52 data_manager_json = dict(data_tables=dict(ncbi_taxonomy=data_manager_entry)) |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
53 params = json.loads(open(args.output).read()) |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
54 target_directory = params['output_data'][0]['extra_files_path'] |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
55 os.mkdir(target_directory) |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
56 output_path = os.path.abspath(os.path.join(os.getcwd(), 'taxonomy')) |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
57 for filename in os.listdir(workdir): |
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
58 shutil.move(os.path.join(output_path, filename), target_directory) |
2
3d628f1e1bef
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 04825a097d217d30deb5fcb6906fe718023e25b6"
iuc
parents:
1
diff
changeset
|
59 with open(args.output, 'w') as out: |
3d628f1e1bef
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 04825a097d217d30deb5fcb6906fe718023e25b6"
iuc
parents:
1
diff
changeset
|
60 out.write(json.dumps(data_manager_json, sort_keys=True)) |
0
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
61 |
1
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
62 |
0
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
63 if __name__ == '__main__': |
1
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
64 parser = argparse.ArgumentParser(description='Create data manager json.') |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
65 parser.add_argument('--out', dest='output', action='store', help='JSON filename') |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
66 parser.add_argument('--name', dest='name', action='store', default=str(datetime.date.today()), help='Data table entry unique ID') |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
67 parser.add_argument('--url', dest='url', action='store', default='ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz', help='Download URL') |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
68 args = parser.parse_args() |
8dd4076813ef
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 8652f36a3a3838dca989426961561e81432acf4f
iuc
parents:
0
diff
changeset
|
69 |
0
078e803565cf
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
devteam
parents:
diff
changeset
|
70 main(args) |