Mercurial > repos > scottx611x > data_manager_fetch_gene_annotation
comparison data_manager/data_manager.py @ 39:2ab076cf69df draft
planemo upload for repository https://scottx611x@toolshed.g2.bx.psu.edu/repos/scottx611x/data_manager_fetch_gene_annotation
author | scottx611x |
---|---|
date | Fri, 08 Jul 2016 11:17:50 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
38:d27c122c2424 | 39:2ab076cf69df |
---|---|
1 import os | |
2 import sys | |
3 import uuid | |
4 import json | |
5 import argparse | |
6 import requests | |
7 from requests.exceptions import ContentDecodingError | |
8 | |
9 | |
10 def url_download(url): | |
11 """Attempt to download gene annotation file from a given url | |
12 :param url: full url to gene annotation file | |
13 :type url: str. | |
14 :returns: name of downloaded gene annotation file | |
15 :raises: ContentDecodingError, IOError | |
16 """ | |
17 response = requests.get(url=url, stream=True) | |
18 | |
19 # Generate file_name | |
20 file_name = response.url.split("/")[-1] | |
21 | |
22 block_size = 10 * 1024 * 1024 # 10MB chunked download | |
23 with open(file_name, 'w+') as f: | |
24 try: | |
25 # Good to note here that requests' iter_content() will | |
26 # automatically handle decoding "gzip" and "deflate" encoding | |
27 # formats | |
28 for buf in response.iter_content(block_size): | |
29 f.write(buf) | |
30 except (ContentDecodingError, IOError) as e: | |
31 sys.stderr.write("Error occured downloading reference file: %s" | |
32 % e) | |
33 os.remove(file_name) | |
34 | |
35 return file_name | |
36 | |
37 | |
38 def main(): | |
39 | |
40 # Generate and parse command line args | |
41 parser = argparse.ArgumentParser(description='Create data manager JSON.') | |
42 parser.add_argument('--out', dest='output', action='store', | |
43 help='JSON filename') | |
44 parser.add_argument('--name', dest='name', action='store', | |
45 default=uuid.uuid4(), help='Data table entry unique ID' | |
46 ) | |
47 parser.add_argument('--url', dest='url', action='store', | |
48 help='Url to download gtf file from') | |
49 | |
50 args = parser.parse_args() | |
51 | |
52 work_dir = os.getcwd() | |
53 | |
54 # Attempt to download gene annotation file from given url | |
55 gene_annotation_file_name = url_download(args.url) | |
56 | |
57 # Update Data Manager JSON and write to file | |
58 data_manager_entry = { | |
59 'data_tables': { | |
60 'gene_annotation': { | |
61 'value': str(args.name), | |
62 'dbkey': str(args.name), | |
63 'name': gene_annotation_file_name, | |
64 'path': os.path.join(work_dir, gene_annotation_file_name) | |
65 } | |
66 } | |
67 } | |
68 | |
69 with open(os.path.join(args.output), "w+") as f: | |
70 f.write(json.dumps(data_manager_entry)) | |
71 | |
72 if __name__ == '__main__': | |
73 main() |