Mercurial > repos > matthias > data_manager_megan_tools
comparison data_manager/data_manager.py @ 0:1f839ba466da draft
planemo upload for repository https://github.com/bernt-matthias/mb-galaxy-tools/tree/master/data_managers/data_manager_megan_tools commit d7a7a198e8f8c9b95491f1520d478e7400a1f59c-dirty
| author | matthias |
|---|---|
| date | Thu, 01 Nov 2018 12:25:53 -0400 |
| parents | |
| children | f7ad11d31098 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:1f839ba466da |
|---|---|
| 1 import argparse | |
| 2 import datetime | |
| 3 import json | |
| 4 import os | |
| 5 import shutil | |
| 6 import tarfile | |
| 7 import zipfile | |
| 8 try: | |
| 9 # For Python 3.0 and later | |
| 10 from urllib.request import Request, urlopen | |
| 11 except ImportError: | |
| 12 # Fall back to Python 2 imports | |
| 13 from urllib2 import Request, urlopen | |
| 14 | |
| 15 FILE2NAME = { | |
| 16 "prot_acc2tax-June2018X1.abin.zip":"Protein accession to NCBI-taxonomy (June2018X1)", | |
| 17 "nucl_acc2tax-June2018.abin.zip":"Nucleotide accession to NCBI-taxonomy (June2018)", | |
| 18 "acc2interpro-June2018X.abin.zip":"Protein accession to InterPro (June2018X)", | |
| 19 "acc2eggnog-Oct2016X.abin.zip":"Protein accession to eggNOG (Oct2016X)", | |
| 20 "acc2seed-May2015XX.abin.zip":"Protein accession to SEED (May2015XX)", | |
| 21 "acc2kegg-Dec2017X1-ue.abin.zip":"Protein accession to KEGG (Dec2017X1). Only for use with the Ultimate Edition of MEGAN.", | |
| 22 "SSURef_Nr99_132_tax_silva_to_NCBI_synonyms.map.gz":"SSURef_Nr99_132_tax_silva_to_NCBI_synonyms.map.gz", | |
| 23 "SSURef_NR99_128_tax_silva_to_NCBI_synonyms.map.gz":"SSURef_NR99_128_tax_silva_to_NCBI_synonyms.map.gz", | |
| 24 "prot_gi2tax-Aug2016X.bin.zip":"Protein accession to NCBI-taxonomy (Aug2016X)", | |
| 25 "nucl_gi2tax-Aug2016.bin.zip":"Nucleotide accession to NCBI-taxonomy (Aug2016)", | |
| 26 "gi2eggnog-June2016X.bin.zip":"Protein accession to InterPro (June2016X)", | |
| 27 "gi2interpro-June2016X.bin.zip":"Protein accession to eggNOG (June2016X)", | |
| 28 "gi2seed-May2015X.bin.zip":"Protein accession to SEED (May2015X)", | |
| 29 "gi2kegg-Aug2016X-ue.bin.zip":"Protein accession to KEGG (Aug2016X). Only for use with the Ultimate Edition of MEGAN." | |
| 30 } | |
| 31 | |
| 32 FILE2TYPE = { | |
| 33 "prot_acc2tax-June2018X1.abin.zip":"acc2tax", | |
| 34 "nucl_acc2tax-June2018.abin.zip":"acc2tax", | |
| 35 "acc2interpro-June2018X.abin.zip":"acc2interpro", | |
| 36 "acc2eggnog-Oct2016X.abin.zip":"acc2eggnog", | |
| 37 "acc2seed-May2015XX.abin.zip":"acc2seed", | |
| 38 "acc2kegg-Dec2017X1-ue.abin.zip":"acc2kegg", | |
| 39 "SSURef_Nr99_132_tax_silva_to_NCBI_synonyms.map.gz":"syn2taxa", | |
| 40 "SSURef_NR99_128_tax_silva_to_NCBI_synonyms.map.gz":"syn2taxa", | |
| 41 "prot_gi2tax-Aug2016X.bin.zip":"gi2tax", | |
| 42 "nucl_gi2tax-Aug2016.bin.zip":"gi2tax", | |
| 43 "gi2eggnog-June2016X.bin.zip":"gi2eggnog", | |
| 44 "gi2interpro-June2016X.bin.zip":"gi2interpro", | |
| 45 "gi2seed-May2015X.bin.zip":"gi2seed-", | |
| 46 "gi2kegg-Aug2016X-ue.bin.zip":"gi2kegg" | |
| 47 } | |
| 48 | |
| 49 def url_download(fname, workdir): | |
| 50 file_path = os.path.join(workdir, 'download.dat') | |
| 51 if not os.path.exists(workdir): | |
| 52 os.makedirs(workdir) | |
| 53 src = None | |
| 54 dst = None | |
| 55 try: | |
| 56 req = Request("http://ab.inf.uni-tuebingen.de/data/software/megan6/download/"+fname) | |
| 57 src = urlopen(req) | |
| 58 with open(file_path, 'wb') as dst: | |
| 59 while True: | |
| 60 chunk = src.read(2**10) | |
| 61 if chunk: | |
| 62 dst.write(chunk) | |
| 63 else: | |
| 64 break | |
| 65 finally: | |
| 66 if src: | |
| 67 src.close() | |
| 68 if zipfile.is_zipfile(file_path): | |
| 69 fh = zipfile.ZipFile(file_path, 'r') | |
| 70 else: | |
| 71 return | |
| 72 fh.extractall(workdir) | |
| 73 os.remove(file_path) | |
| 74 | |
| 75 | |
| 76 def main(fname, outjson): | |
| 77 workdir = os.path.join(os.getcwd(), 'megan_tools') | |
| 78 url_download(fname, workdir) | |
| 79 | |
| 80 data_manager_entry = {} | |
| 81 data_manager_entry['value'] = fname.split(".")[0] | |
| 82 data_manager_entry['name'] = FILE2NAME[fname] | |
| 83 data_manager_entry['type'] = FILE2TYPE[fname] | |
| 84 data_manager_entry['path'] = '.' | |
| 85 | |
| 86 data_manager_json = dict(data_tables=dict(megan_tools=data_manager_entry)) | |
| 87 | |
| 88 params = json.loads(open(outjson).read()) | |
| 89 target_directory = params['output_data'][0]['extra_files_path'] | |
| 90 os.mkdir(target_directory) | |
| 91 output_path = os.path.abspath(os.path.join(os.getcwd(), 'megan_tools')) | |
| 92 for filename in os.listdir(workdir): | |
| 93 shutil.move(os.path.join(output_path, filename), target_directory) | |
| 94 file(outjson, 'w').write(json.dumps(data_manager_json)) | |
| 95 | |
| 96 | |
| 97 if __name__ == '__main__': | |
| 98 parser = argparse.ArgumentParser(description='Create data manager json.') | |
| 99 parser.add_argument('--out', action='store', help='JSON filename') | |
| 100 parser.add_argument('--file', action='store', help='Download filename') | |
| 101 args = parser.parse_args() | |
| 102 | |
| 103 main(args.file, args.out) |
