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