Mercurial > repos > iuc > data_manager_mitos
comparison data_manager/data_manager.py @ 0:26a6f95e82c8 draft
"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_mitos commit 791f28c8a7194fdd1ecec05ad166932d461899b2"
author | iuc |
---|---|
date | Fri, 27 Mar 2020 17:50:23 -0400 |
parents | |
children | 1ff9996995ee |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:26a6f95e82c8 |
---|---|
1 import argparse | |
2 import json | |
3 import os | |
4 import shutil | |
5 import tarfile | |
6 try: | |
7 # For Python 3.0 and later | |
8 from urllib.request import Request, urlopen | |
9 except ImportError: | |
10 # Fall back to Python 2 imports | |
11 from urllib2 import Request, urlopen | |
12 | |
13 ZENODO = { | |
14 "mitos": "2683856", | |
15 "mitos2": "3685310" | |
16 } | |
17 NAMES = { | |
18 "mitos1-refdata": "RefSeq39 + MiTFi tRNA models", | |
19 "refseq39": "RefSeq39 (equivalent to MITOS1 data)", | |
20 "refseq63m": "RefSeq63 Metazoa", | |
21 "refseq63f": "RefSeq63 Fungi", | |
22 "refseq63o": "RefSeq63 Opisthokonta", | |
23 "refseq89m": "RefSeq89 Metazoa", | |
24 "refseq89f": "RefSeq89 Fungi", | |
25 "refseq89o": "RefSeq89 Opisthokonta" | |
26 } | |
27 | |
28 | |
29 def url_download(tpe, db, workdir): | |
30 """ | |
31 download http://ab.inf.uni-tuebingen.de/data/software/megan6/download/FNAME | |
32 to workdir | |
33 and unzip | |
34 | |
35 return the name of the resulting dir | |
36 """ | |
37 tarfname = os.path.join(workdir, db + ".tar.bz") | |
38 if not os.path.exists(workdir): | |
39 os.makedirs(workdir) | |
40 src = None | |
41 dst = None | |
42 try: | |
43 req = Request("https://zenodo.org/record/{zenodoid}/files/{db}.tar.bz2?download=1".format(zenodoid=ZENODO[tpe], db=db)) | |
44 src = urlopen(req) | |
45 with open(tarfname, 'wb') as dst: | |
46 while True: | |
47 chunk = src.read(2**10) | |
48 if chunk: | |
49 dst.write(chunk) | |
50 else: | |
51 break | |
52 finally: | |
53 if src: | |
54 src.close() | |
55 with tarfile.open(tarfname, "r:bz2") as tar: | |
56 dirname = tar.getnames()[0] | |
57 tar.extractall(workdir) | |
58 os.remove(tarfname) | |
59 return dirname | |
60 | |
61 | |
62 def main(tpe, db, outjson): | |
63 workdir = os.getcwd() | |
64 | |
65 path = url_download(tpe, db, workdir) | |
66 | |
67 data_manager_entry = {} | |
68 data_manager_entry['value'] = db | |
69 data_manager_entry['name'] = NAMES[db] | |
70 data_manager_entry['type'] = tpe | |
71 data_manager_entry['path'] = path | |
72 data_manager_json = dict(data_tables=dict(mitos=data_manager_entry)) | |
73 | |
74 with open(outjson) as f: | |
75 params = json.loads(f.read()) | |
76 target_directory = params['output_data'][0]['extra_files_path'] | |
77 os.mkdir(target_directory) | |
78 # output_path = os.path.abspath(os.path.join(os.getcwd(), 'mitos')) | |
79 shutil.move(os.path.join(workdir, path), target_directory) | |
80 with open(outjson, 'w') as fh: | |
81 fh.write(json.dumps(data_manager_json, sort_keys=True)) | |
82 | |
83 | |
84 if __name__ == '__main__': | |
85 parser = argparse.ArgumentParser(description='Create data manager json.') | |
86 parser.add_argument('--out', action='store', help='JSON filename') | |
87 parser.add_argument('--type', action='store', help='mitos version') | |
88 parser.add_argument('--db', action='store', help='db name') | |
89 args = parser.parse_args() | |
90 | |
91 main(args.type, args.db, args.out) |