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