Mercurial > repos > devteam > data_manager_fetch_ncbi_taxonomy
annotate data_manager/data_manager.py @ 4:d6acd5cd497b 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 | Mon, 06 Jul 2020 20:06:21 -0400 |
parents | bdd8632fa9fb |
children | 9de2fb1ce1fb |
rev | line source |
---|---|
0
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
1 import argparse |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
2 import datetime |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
3 import json |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
4 import os |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
5 import shutil |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
6 import tarfile |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
7 import zipfile |
4
d6acd5cd497b
"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: |
d6acd5cd497b
"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 |
d6acd5cd497b
"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 |
d6acd5cd497b
"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: |
d6acd5cd497b
"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 |
d6acd5cd497b
"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
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
14 |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
15 |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
16 def url_download(url, workdir): |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
17 file_path = os.path.join(workdir, 'download.dat') |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
18 if not os.path.exists(workdir): |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
19 os.makedirs(workdir) |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
20 src = None |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
21 dst = None |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
22 try: |
1
926847693e4d
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) |
926847693e4d
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) |
926847693e4d
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: |
926847693e4d
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: |
926847693e4d
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) |
926847693e4d
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: |
926847693e4d
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) |
926847693e4d
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: |
926847693e4d
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
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
32 finally: |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
33 if src: |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
34 src.close() |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
35 if tarfile.is_tarfile(file_path): |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
36 fh = tarfile.open(file_path, 'r:*') |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
37 elif zipfile.is_zipfile(file_path): |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
38 fh = zipfile.ZipFile(file_path, 'r') |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
39 else: |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
40 return |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
41 fh.extractall(workdir) |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
42 os.remove(file_path) |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
43 |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
44 |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
45 def main(args): |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
46 workdir = os.path.join(os.getcwd(), 'taxonomy') |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
47 url_download(args.url, workdir) |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
48 data_manager_entry = {} |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
49 data_manager_entry['value'] = args.name.lower() |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
50 data_manager_entry['name'] = args.name |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
51 data_manager_entry['path'] = '.' |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
52 data_manager_json = dict(data_tables=dict(ncbi_taxonomy=data_manager_entry)) |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
53 params = json.loads(open(args.output).read()) |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
54 target_directory = params['output_data'][0]['extra_files_path'] |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
55 os.mkdir(target_directory) |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
56 output_path = os.path.abspath(os.path.join(os.getcwd(), 'taxonomy')) |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
57 for filename in os.listdir(workdir): |
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
58 shutil.move(os.path.join(output_path, filename), target_directory) |
2
4af4e175db0d
"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: |
4af4e175db0d
"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
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
61 |
1
926847693e4d
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
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
63 if __name__ == '__main__': |
1
926847693e4d
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.') |
926847693e4d
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') |
926847693e4d
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') |
926847693e4d
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') |
926847693e4d
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() |
926847693e4d
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
fafcd2f5db36
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 220e70acb66016873732b193b3ac20ae48083f0b-dirty
devteam
parents:
diff
changeset
|
70 main(args) |