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