comparison data_manager/data_manager.py @ 46:9346d2955707 draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_gene_annotation/ commit 8652f36a3a3838dca989426961561e81432acf4f
author iuc
date Tue, 04 Apr 2017 18:07:39 -0400
parents
children 3a02b8ab423a
comparison
equal deleted inserted replaced
45:16e961b2f35c 46:9346d2955707
1 import argparse
2 import datetime
3 import json
4 import os
5 import sys
6 import uuid
7
8 import requests
9 from requests.exceptions import ContentDecodingError
10
11
12 def url_download(url):
13 """Attempt to download gene annotation file from a given url
14 :param url: full url to gene annotation file
15 :type url: str.
16 :returns: name of downloaded gene annotation file
17 :raises: ContentDecodingError, IOError
18 """
19 response = requests.get(url=url, stream=True)
20
21 # Generate file_name
22 file_name = response.url.split("/")[-1]
23
24 block_size = 10 * 1024 * 1024 # 10MB chunked download
25 with open(file_name, 'w+') as f:
26 try:
27 # Good to note here that requests' iter_content() will
28 # automatically handle decoding "gzip" and "deflate" encoding
29 # formats
30 for buf in response.iter_content(block_size):
31 f.write(buf)
32 except (ContentDecodingError, IOError) as e:
33 sys.stderr.write("Error occured downloading reference file: %s"
34 % e)
35 os.remove(file_name)
36
37 return file_name
38
39
40 def main():
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 'gff_gene_annotations': {
61 'value': str(datetime.datetime.now()),
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
73 if __name__ == '__main__':
74 main()