Mercurial > repos > devteam > data_manager_fetch_ncbi_taxonomy
annotate data_manager/data_manager.py @ 0:078e803565cf draft
planemo upload for repository https://github.com/galaxyproject/tools-devteam/blob/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 86cf90107482cab1cb47fc0d42d6705f8077daa7
author | devteam |
---|---|
date | Fri, 06 Nov 2015 14:16:28 -0500 |
parents | |
children | 8dd4076813ef |
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 sys |
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 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
|
8 import urllib2 |
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
|
9 import zipfile |
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
|
10 |
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
|
11 parser = argparse.ArgumentParser(description='Create data manager 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
|
12 parser.add_argument('--out', dest='output', action='store', help='JSON filename') |
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
|
13 parser.add_argument('--name', dest='name', action='store', default=str(datetime.date.today()), help='Data table entry unique ID') |
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 parser.add_argument('--url', dest='url', action='store', default='ftp://ftp.ncbi.nih.gov/pub/taxonomy/taxdump.tar.gz', help='Download URL') |
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 args = parser.parse_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
|
17 |
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 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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 try: |
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
|
25 req = urllib2.Request(url) |
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
|
26 src = urllib2.urlopen(req) |
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
|
27 dst = open(file_path, 'wb') |
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
|
28 while True: |
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
|
29 chunk = src.read(2**10) |
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
|
30 if chunk: |
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
|
31 dst.write(chunk) |
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 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
|
33 break |
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 except Exception, e: |
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 print >>sys.stderr, str(e) |
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 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
|
37 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
|
38 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
|
39 if dst: |
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 dst.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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 |
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 |
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 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 shutil.move(os.path.join(output_path, filename), 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
|
65 file(args.output, 'w').write(json.dumps(data_manager_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
|
66 |
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
|
67 if __name__ == '__main__': |
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
|
68 main(args) |