Mercurial > repos > iuc > data_manager_fetch_busco
comparison data_manager/data_manager.py @ 2:2b4526fdf7fb draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_busco/ commit 2d2c72dc464b938bfa4def2511ce0938f3a1ea7d
author | iuc |
---|---|
date | Mon, 24 Apr 2023 12:26:46 +0000 |
parents | 15b97817550a |
children |
comparison
equal
deleted
inserted
replaced
1:15b97817550a | 2:2b4526fdf7fb |
---|---|
1 #!/usr/bin/env python | |
2 # | |
3 # Data manager for reference data for the 'BUSCO' Galaxy tools | |
1 import argparse | 4 import argparse |
2 import datetime | 5 import datetime |
3 import json | 6 import json |
4 import os | 7 import os |
5 import shutil | 8 import shutil |
6 import tarfile | 9 import subprocess |
7 import zipfile | 10 from pathlib import Path |
8 try: | |
9 # For Python 3.0 and later | |
10 from urllib.request import Request, urlopen | |
11 except ImportError: | |
12 # Fall back to Python 2 imports | |
13 from urllib2 import Request, urlopen | |
14 | |
15 | |
16 def url_download(url, workdir): | |
17 file_path = os.path.join(workdir, 'download.dat') | |
18 if not os.path.exists(workdir): | |
19 os.makedirs(workdir) | |
20 src = None | |
21 dst = None | |
22 try: | |
23 req = Request(url) | |
24 src = urlopen(req) | |
25 with open(file_path, 'wb') as dst: | |
26 while True: | |
27 chunk = src.read(2**10) | |
28 if chunk: | |
29 dst.write(chunk) | |
30 else: | |
31 break | |
32 finally: | |
33 if src: | |
34 src.close() | |
35 if tarfile.is_tarfile(file_path): | |
36 fh = tarfile.open(file_path, 'r:*') | |
37 elif zipfile.is_zipfile(file_path): | |
38 fh = zipfile.ZipFile(file_path, 'r') | |
39 else: | |
40 return | |
41 fh.extractall(workdir) | |
42 os.remove(file_path) | |
43 | 11 |
44 | 12 |
45 def main(args): | 13 def main(args): |
46 workdir = os.path.join(os.getcwd(), 'busco') | 14 workdir = os.path.join(os.getcwd(), "busco_downloads") |
47 url_download(args.url, workdir) | 15 cmd = "busco --download %s" % args.database |
16 subprocess.check_call(cmd, shell=True) | |
17 with open(args.json) as fh: | |
18 params = json.load(fh) | |
19 target_directory = params["output_data"][0]["extra_files_path"] | |
48 data_manager_entry = {} | 20 data_manager_entry = {} |
49 data_manager_entry['value'] = args.name.lower() | 21 data_manager_entry["value"] = args.name.lower() |
50 data_manager_entry['name'] = args.name | 22 data_manager_entry["name"] = args.name |
51 data_manager_entry['path'] = '.' | 23 data_manager_entry["version"] = args.version |
52 data_manager_json = dict(data_tables=dict(busco=data_manager_entry)) | 24 data_manager_entry["path"] = str(Path(target_directory)) |
53 with open(args.output) as fh: | 25 data_manager_json = dict(data_tables=dict(busco_database=data_manager_entry)) |
54 params = json.load(fh) | 26 |
55 target_directory = params['output_data'][0]['extra_files_path'] | |
56 os.mkdir(target_directory) | 27 os.mkdir(target_directory) |
57 output_path = os.path.abspath(os.path.join(os.getcwd(), 'busco')) | 28 output_path = os.path.abspath(os.path.join(os.getcwd(), "busco_downloads")) |
58 for filename in os.listdir(workdir): | 29 for filename in os.listdir(workdir): |
59 shutil.move(os.path.join(output_path, filename), target_directory) | 30 shutil.move(os.path.join(output_path, filename), target_directory) |
60 with open(args.output, 'w') as fh: | 31 with open(args.json, "w") as fh: |
61 json.dump(data_manager_json, fh, sort_keys=True) | 32 json.dump(data_manager_json, fh, sort_keys=True) |
62 | 33 |
63 | 34 |
64 if __name__ == '__main__': | 35 if __name__ == "__main__": |
65 parser = argparse.ArgumentParser(description='Create data manager json.') | 36 |
66 parser.add_argument('--out', dest='output', action='store', help='JSON filename') | 37 # Read command line |
67 parser.add_argument('--name', dest='name', action='store', default=str(datetime.date.today()), help='Data table entry unique ID') | 38 parser = argparse.ArgumentParser(description="Download BUSCO database") |
68 parser.add_argument('--url', dest='url', action='store', help='Download URL') | 39 parser.add_argument("--database", help="Database name") |
40 parser.add_argument( | |
41 "--name", default=str(datetime.date.today()), help="Data table entry unique ID" | |
42 ) | |
43 parser.add_argument("--version", help="BUSCO version") | |
44 parser.add_argument("--json", help="Path to JSON file") | |
69 args = parser.parse_args() | 45 args = parser.parse_args() |
70 | 46 |
71 main(args) | 47 main(args) |