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