40
|
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': {
|
41
|
61 'value': gene_annotation_file_name,
|
|
62 'dbkey': gene_annotation_file_name.split(".")[0],
|
|
63 'name': str(args.name),
|
40
|
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()
|