annotate data_manager/data_manager.py @ 3:bdd8632fa9fb draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 1bcd553f81064957d5786302361675c29ed58a8d"
author iuc
date Mon, 06 Jul 2020 19:32:35 -0400
parents 4af4e175db0d
children d6acd5cd497b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
3
bdd8632fa9fb "planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_ncbi_taxonomy/ commit 1bcd553f81064957d5786302361675c29ed58a8d"
iuc
parents: 2
diff changeset
8 from urllib.request 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
9
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 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
12 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
13 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
14 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
15 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
16 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
17 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
18 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
19 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
20 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
21 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
22 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
23 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
24 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
25 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
26 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
27 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
28 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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 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
37 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
38
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
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 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
41 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
42 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
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55 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
56
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
57
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
58 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
59 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
60 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
61 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
62 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
63 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
64
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
65 main(args)